package com.yami.shop.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.model.ExtensionWorker;
import com.yami.shop.bean.model.User;
import com.yami.shop.common.util.PageAdapter;
import com.yami.shop.dao.ExtensionMapper;
import com.yami.shop.service.ExtensionService;
import com.yami.shop.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ExtensionServiceImpl extends ServiceImpl<ExtensionMapper, ExtensionWorker> implements  ExtensionService {

    @Autowired
    private ExtensionMapper extensionMapper;

    @Autowired
    private UserService userService;

    @Autowired
    private ExtensionService extensionService;

    @Override
    public ExtensionWorker getExtensionWorker(String userId) {
        return extensionMapper.getExtensionWorker(userId);
    }

    @Override
    public String updateExtension(Long promotionId, String status) {
        String result = "success";
        ExtensionWorker extensionWorker = extensionService.getById(promotionId);
        String userId = extensionWorker.getUserId();
        User user = userService.getUserByUserId(userId);
        if(status.equals("2")){
            extensionWorker.setStatus(status);
            extensionWorker.setExtensionWorker("0");
            extensionWorker.setPhone(user.getUserMobile());
            extensionWorker.setPic(user.getPic());
            extensionWorker.setNickName(user.getNickName());
            extensionService.saveOrUpdate(extensionWorker);
            //更新用户表中推广员字段

            user.setExtensionWorker("0");
            userService.saveOrUpdate(user);
        }else if(status.equals("3")){
            extensionWorker.setStatus(status);
            extensionService.saveOrUpdate(extensionWorker);
        }
        return result;
    }

    @Override
    public IPage<ExtensionWorker> pageExtensionWork(Page<ExtensionWorker> page, ExtensionWorker extensionWorker) {
        page.setRecords(extensionMapper.listExtensionWork(new PageAdapter(page),extensionWorker));
        page.setTotal(extensionMapper.workCountAccount(extensionWorker));
        return page;
    }

}
